from bluedot.settings import KUAIDI_KEY, KUAIDI_CUSTOMER
import json, hashlib, requests

KUAIDI_STATES = {
    0: '在途', 1: '揽收', 2: '疑难', 3: '签收', 4: '退签', 5: '派件', 6: '退回', 7: '转单', 10: '待清关',
    11: '清关中', 12: '已清关', 13: '清关异常', 14: '收件人拒签'
}


def get_kuaidi_info(com, num, phone):
    params = {
        "com": com,
        "num": num,
        "phone": phone,
    }

    pjson = json.dumps(params)

    postdata = {
        "param": pjson,
        "customer": KUAIDI_CUSTOMER,
    }
    sign = md5_encrypt(pjson + KUAIDI_KEY + KUAIDI_CUSTOMER).upper()
    postdata["sign"] = sign  # 加密签名

    resp = requests.post("http://poll.kuaidi100.com/poll/query.do", data=postdata).json()

    return resp


def md5_encrypt(content):
    md5 = hashlib.md5()
    md5.update(content.encode())
    return md5.hexdigest()


if __name__ == '__main__':
    data = get_kuaidi_info("yuantong", "123456", "18600863930")
    print(data)
    print(data["message"])
